#include "Engine.h"

void Engine::Start()
{	
	//Initalize COM
	if(FAILED(CoInitialize(NULL)))
	{
		MessageBox(NULL, "Failed to initalize COM", "Error!", MB_OK | MB_ICONERROR);
	}

	Sys::Instance().Init();
	FileSystem::Instance().Init();

	//before starting render menager we need fill information structure
	GD::_ED.numViewports = 1;
	GD::_ED.windowed = true;

	RenderMgr::Instance().Init();

	//Start engine main timer afret all main initalizations
	engineTimer.Reset();
	engineTimer.Start();

}

void Engine::Shutdown()
{
	engineTimer.Stop();

	Sys::Instance().Shutdown();
	FileSystem::Instance().Shutdown();
	RenderMgr::Instance().Shutdown();

	CoUninitialize();

	SendMessage(GD::_ED.hWnd, WM_CLOSE, 0, 0);
}

LRESULT Engine::EngMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		{
			DestroyWindow(hWnd);
			break;
		}
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
			break;
		}
	case WM_SIZE:
		{
			GD::_ED.windowWidth = LOWORD(lParam);
			GD::_ED.windowHeight = HIWORD(lParam);
			RenderMgr::Instance().OnResize();
		}
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

void Engine::Update()
{
	//Main loop of engine
	engineTimer.Tick();

	//First updates
	RenderMgr::Instance().Update(engineTimer);

	//Next drawing
	RenderMgr::Instance().Draw();

	

}


